package com.ruoyi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.pojo.*;
import com.ruoyi.service.*;
import com.ruoyi.utils.LoansUtils;
import com.ruoyi.vo.SigninRecord;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.redisson.Redisson;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.jar.JarOutputStream;

@RestController
@RequestMapping("/personage")
public class PersonageController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RedissonClient redissonClient;

    @Resource
    private AppService appService;
    @Autowired
    private LoansService loansService;

    @Autowired
    private MessageService messageService;

    @Autowired
    private UserSigninService userSigninService;

    @Autowired
    private UesrService uesrService;

    @Autowired
    private PrizeService prizeService;

    @Autowired
    private CarryService carryService;

    //查询贷款信息
    @PostMapping("/loanslist")
    public List<Loans> loansList(HttpServletResponse response){
        //获取用户id
//        String token = response.getHeader("token");
//        Integer tokenUid = appService.tokenUserId(token);
        QueryWrapper<Loans> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",1);
        List<Loans> list = loansService.list(queryWrapper);
        return list;
    }

    //还款信息
    @PostMapping("/messagelist")
    public List<Message> messageList(@RequestParam("lid") Integer lid){
        QueryWrapper<Message> wrapper = new QueryWrapper<>();
        wrapper.eq("lid",lid);
        List<Message> list = messageService.list(wrapper);
        return list;
    }

    //查看签到记录
    @PostMapping("/signinlist")
    public List<SigninRecord> signinRecordList(HttpServletResponse response){
        //获取用户id
//        String token = response.getHeader("token");
//        Integer tokenUid = appService.tokenUserId(token);
        List<SigninRecord> signinList = appService.signinRecordList(1);
        return signinList;
    }

    //用户签到
    @PostMapping("/signin")
    public Boolean sigin(HttpServletResponse response,@RequestParam("sid") Integer sid){
        //获取用户id
//        String token = response.getHeader("token");
//        Integer tokenUid = appService.tokenUserId(token);
        Integer uid = 1;
        //查询用户是否已经签过到
        Integer sint = appService.signinRecord(uid,sid);
        if (sint>0){
            return false;
        }
        //添加签到记录
        UserSignin userSignin = new UserSignin();
        userSignin.setUid(uid);
        userSignin.setSid(sid);
        userSignin.setUtime(new Date());
        userSigninService.save(userSignin);
        //修改用户积分
        QueryWrapper<Uesr> uesrQueryWrapper = new QueryWrapper<>();
        uesrQueryWrapper.eq("uid",uid);
        List<Uesr> list = uesrService.list(uesrQueryWrapper);
        Uesr uesr = list.get(0);
        Integer unum = uesr.getUnum();
        uesr.setUnum(unum + 10);
        uesrService.update(uesr,uesrQueryWrapper);
        return true;
    }

    //抽奖
    @PostMapping("/extract")
    public Boolean extract(HttpServletResponse response){
        //获取用户id
//        String token = response.getHeader("token");
//        Integer tokenUid = appService.tokenUserId(token);
        Integer uid = 1;
        //上锁
        RLock numRedissonLock = redissonClient.getLock(uid+"");
        Boolean b = false;
        try {
            //指定上锁时间
            numRedissonLock.lock(3, TimeUnit.MINUTES);
            //获取用户积分
            QueryWrapper<Uesr> uesrQueryWrapper = new QueryWrapper<>();
            uesrQueryWrapper.eq("uid",uid);
            List<Uesr> list = uesrService.list(uesrQueryWrapper);
            Uesr uesr = list.get(0);
            Integer unum = uesr.getUnum();
            //判断剩余积分是否足够
            if (unum<10){
                b = false;
                System.out.println("积分不足");
                return false;
            }
            //减少用户积分
            unum = unum - 10;
            uesr.setUnum(unum);
            uesrService.update(uesr,uesrQueryWrapper);
            //抽奖
            //查询数量不为0的奖品数
            b = appService.lotteryRaffle(uesr);
            return false;
        }finally {
            //解锁
            numRedissonLock.unlock();
            return b;
        }
    }

    //布隆过滤器
    @PostMapping("/bl")
    public Boolean bl(@RequestParam("aaa") String aaa,@RequestParam("bbb") String bbb){
        RBloomFilter<Object> user = redissonClient.getBloomFilter("user");
        user.tryInit(10000,0.01);
        if (aaa!=null){
            user.add(aaa);
        }
        if (user.contains(bbb)){
            return true;
        }
        return false;
    }

    @PostMapping("/lx")
    public void lx(){
        long invest = 10000; // 本金
        int month = 12;
        double yearRate = 0.12; // 年利率
        long perMonthPrincipalInterest = LoansUtils.getPerMonthPrincipalInterest(invest, yearRate, month);
        System.out.println("等额本息---每月还款本息：" + perMonthPrincipalInterest);
        Map<Integer, Long> mapInterest = LoansUtils.getPerMonthInterest(invest, yearRate, month);
        System.out.println("等额本息---每月还款利息：" + mapInterest);
        Map<Integer, Long> mapPrincipal = LoansUtils.getPerMonthPrincipal(invest, yearRate, month);
        System.out.println("等额本息---每月还款本金：" + mapPrincipal);
        long count = LoansUtils.getInterestCount(invest, yearRate, month);
        System.out.println("等额本息---总利息：" + count);


        for(int i=1;i<month + 1;i++){
            if(i<month){
                System.out.println("等额本息---"+i+"月还款本金:" + mapPrincipal.get(i) + ",还款利息:" + mapInterest.get(i));
                invest = invest - mapPrincipal.get(i);
            } else {
                System.out.println("等额本息---"+i+"月还款本金:" +invest + ",还款利息:" + (perMonthPrincipalInterest - invest));
            }

        }
        for (int i = 1; i <= 12; i++) {
            LoansUtils.equalityEorpus(10000,12,i,0.01);
        }
    }
}
